package com.jdjw.modules.officialseal.controller;


import com.jdjw.common.ResponseResult;
import com.jdjw.modules.officialseal.entity.OfficialSeal;
import com.jdjw.modules.officialseal.entity.OfficialSealSave;
import com.jdjw.modules.officialseal.entity.OfficialSealVo;
import com.jdjw.modules.officialseal.mapper.OfficialSealMapper;
import com.jdjw.modules.officialseal.service.OfficialSealService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 公章使用审核 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/officialseal")
public class OfficialSealController {

    @Autowired
    private OfficialSealService officialSealServicel;

    @Autowired
    private OfficialSealMapper officialSealMapper;


    @ApiModelProperty("公章使用-查询")
    @PostMapping("queryAll")
    public ResponseResult list(OfficialSealVo officialSealVo){
        return officialSealServicel.queryAll(officialSealVo);
    }

    @ApiModelProperty("公章使用-添加")
    @PostMapping("save")
    public ResponseResult save(OfficialSealSave officialSealSave){
        ResponseResult rrt=new ResponseResult();
        OfficialSeal officialSeal=new OfficialSeal();
        BeanUtils.copyProperties(officialSealSave,officialSeal);
        SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        officialSeal.setCreateTime(s.format(new Date()));
        //判断事项清单是否包含这些（包含不添加）
        List<String> slist=new ArrayList<>();
        slist.add("亲属关系"); slist.add("户籍证明");
        slist.add("户口登记"); slist.add("养犬");
        slist.add("无犯罪记录"); slist.add("社区戒毒");
        slist.add("人员失踪"); slist.add("婚姻");
        slist.add("出生"); slist.add("健在");
        slist.add("死亡"); slist.add("疾病");
        slist.add("残疾"); slist.add("婚育");
        slist.add("就业"); slist.add("个人档案");
        slist.add("财产"); slist.add("遗产继承");
        slist.add("证件丢失");
        for (String sk : slist) {
            if(officialSeal.getSealSxqd().equals(sk)){
                rrt.setCode(500);
                rrt.setMsg("清单不符合申请条件！");
                return rrt;
            }
        }
        ArrayList<HashMap<String, String>> hashMaps =   officialSealMapper.selectCodeAndaid(officialSeal.getDept());
        if(hashMaps.size()>0){
            officialSeal.setAttribute01(hashMaps.get(0).get("code"));//镇id
            officialSeal.setAttribute02(hashMaps.get(0).get("aid"));//村id
        }
        boolean save = officialSealServicel.save(officialSeal);
        if(save){
            rrt.setCode(200);
            rrt.setMsg("保存成功");
        }else{
            rrt.setCode(500);
            rrt.setMsg("保存失败");
        }
        return rrt;
    }
    @ApiModelProperty("公章使用-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {

        JSONObject json=new JSONObject(sqlEdit);
        return officialSealServicel.editSql(json.getString("sqlEdit"));
    }

    @ApiModelProperty("公章使用-删除")
    @PostMapping("delete")
    public ResponseResult delete(@ApiParam(value = "公章id",required = true) String id){
        ResponseResult rrt=new ResponseResult();
        boolean b = officialSealServicel.removeById(id);
        if(b){
            rrt.setCode(200);
            rrt.setMsg("删除成功");
        }else{
            rrt.setCode(500);
            rrt.setMsg("删除失败");
        }
        return rrt;
    }

    @ApiModelProperty("公章使用-查看")
    @PostMapping("{id}/preview")
    public ResponseResult preview(@ApiParam(value = "公章id",required = true)@PathVariable("id")String id){
        ResponseResult rrt=new ResponseResult();
        OfficialSeal byId = officialSealServicel.getById(id);
        if(!StringUtils.isEmpty(byId)){
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(byId);
        }else{
            rrt.setCode(500);
            rrt.setMsg("查看失败");
            rrt.setData(null);
        }
        return rrt;
    }

}

